import { defineStore } from "pinia";
import { PAUSE, F } from '@/utils/global'

export const useRecordStore = defineStore('record', {
  state: () => ({
    pause: {
      status: false,
      visible: false,
      prog: 0
    },
    /**
     * 游戏进行到某个阶段period以及对应的svg背景色
     * 玩家选择 - select - #000
     * 过场幕布 - stage - #757575
     * 游戏进行 - gaming - #757575
     * 游戏结束 - over - #757575
     * 游戏结算 - settlement - #000
     */
    period: 'select',
    background: '#000',
    mode: 'single', // single or double
    controller: 'key-mouse', // or mobile
    currentAliveEnemyCount: 0, // 当前关卡在战场中的敌人数量
    remainEnemyCount: 20, // 当前关卡还未刷新倒战场的敌人总数
    stage: {
      count: 0, // 当前关卡数
      time: 30 * F, // 关闭、停留、打开，每个阶段所用时间
      prog: 0,
      height: 0,
      status: 'close',
      visible: false, // stage xx 过场文案是否显示
    },
    eagleAlive: false,
  }),
  actions: {
    updateBackground(type) {
      this.background = type ? '#757575' : '#000'
    },
    initPause() {
      if(this.pause.status === false) return
      this.pause.status = false
      this.pause.visible = false
      this.pause.prog = 0
    },
    goPause() {
      this.pause.status = true
      this.pause.visible = true
    },
    switchPause() {
      if (this.pause.status) {
        this.initPause()
      } else {
        this.goPause()
      }
    },
    pauseAnimation(ft) {
      if(!this.pause.status) return
      if (this.pause.prog >= PAUSE.interval) {
        this.pause.visible = !this.pause.visible
        this.pause.prog = 0
      } else {
        this.pause.prog += ft
      }
    },
    updateStageCount() {
      this.stage.count++
    },
    stageAnim(ft) {
      if (this.period !== 'stage') return
      if (this.stage.status === 'close') {
        if (this.stage.prog >= this.stage.time) {
          this.stage.status = 'stay'
          this.stage.prog = 0
        } else {
          this.stage.prog += ft
          const h = Math.min(this.stage.prog / this.stage.time, 1)
          this.stage.height = h
          if (h >= 0.75) {
            this.stage.visible = true
          }
        }
      } else if (this.stage.status === 'stay') {
        if (this.stage.prog >= this.stage.time) {
          this.stage.status = 'open'
          this.stage.prog = 0
        } else {
          this.stage.prog += ft
        }
      } else if (this.stage.status === 'open') {
        if (this.stage.prog >= this.stage.time) {
          this.stage.status = 'close'
          this.stage.prog = 0
          this.stage.height = 0
          this.period = 'gaming'
        } else {
          this.stage.prog += ft
          this.stage.height = Math.max((this.stage.time - this.stage.prog) / this.stage.time, 0)
          this.stage.visible = false
        }
      }
    }
  }
})